<?php

namespace app\admin\controller;

use think\Lang;

/**
  优惠劵赠送记录
 * ============================================================================
 * DSMall多用户商城
 * ============================================================================
 * 版权所有 2014-2028 长沙德尚网络科技有限公司，并保留所有权利。
 * 网站地址: http://www.csdeshang.com
 * ----------------------------------------------------------------------------
 * 这不是一个自由软件！您只能在不用于商业目的的前提下对程序代码进行修改和使用 .
 * 不允许对程序代码以任何形式任何目的的再发布。
 * ============================================================================
 * 控制器
 */
class Vouchersend extends AdminControl {

    public function _initialize() {
        parent::_initialize();
        Lang::load(APP_PATH . 'admin/lang/'.config('default_lang').'/promotionmgdiscount.lang.php');
    }

    /**
     * 列表
     */
    public function index() {
        $param = array();

        if (trim(input('param.name'))) {
            $title_name = input('param.name');
            $param['voucher_name'] = array('like', "%$title_name%");
        }
        if (trim(input('param.member_name'))) {
            $member_name = input('param.member_name');
            $param['member_name'] = array('like', "%$member_name%");
        }

        $state = intval(input('param.state'));
        if ($state) {
            $param['voucher_type_id'] = $state;
        }
        
        $param['vc.is_send_del'] = 0;
        $voucher_model = model('voucher');
        $vouchertype_list = $voucher_model->getVouchersendList($param, '', '', 10, 'id desc');

        $this->assign('show_page', $voucher_model->page_info->render());

        $this->assign('vouchertype_list', $vouchertype_list);

        $this->assign('name', input('param.name'));
        $this->assign('state', input('param.state'));
        $this->assign('member_name', input('param.member_name'));

        // 输出自营店铺IDS
        $this->assign('flippedOwnShopIds', array_flip(model('store')->getOwnShopIds()));
        $this->setAdminCurItem('index');
        return $this->fetch();
    }


    /**
     * 新增
     */
    public function vouchertype_add() {
        if (!(request()->isPost())) {
            $data = [
                'full_price' => '',
                'ap_id' => '',
                'status' => '1',
                'send_name' => ''
            ];
            $this->assign('adv',$data);
            //获取所有优惠劵
            $where = [
               // 'voucher_state' => 1,
               'status' => 1,
               'is_del' => 0
            ];
            $voucher = db('voucher')->where($where)->select();
            $this->assign('voucher',$voucher);
            return $this->fetch();
        } else {
            $data = input('post.');
            if(empty($data['send_name'])){
                return json(['code' => 0, 'msg' => '优惠券赠送类型不能为空']);
            }
            // var_dump($data);exit;
            if ($data['vouchertype'] < 0) {
                return json(['code' => 0, 'msg' => '参数错误']);
                // $this->error('参数错误');
            }
            
            //当选择首次登陆的时候，判断首次登陆有一个
            if($data['vouchertype'] == 1){
                $where = [
                    'pattern_id' => 1,
                    'is_del' => 0
                ];
                $first_login = db('voucher_type')->where($where)->count();
                if($first_login >= 1){
                    return json(['code' => 0, 'msg' => '新用户注册首次登录赠送只能添加一次']);
                    // $this->error('新用户注册首次登录赠送只能添加一次');
                }
            }

            $param_insert['send_name'] = $data['send_name'];
            $param_insert['voucher_id'] = $data['voucher'];
            $param_insert['pattern_id'] = $data['vouchertype'];
            switch ($data['vouchertype']) {
                case 1:
                    $param_insert['pattern_name'] = '新用户注册首次登录赠送';
                    break;
                case 2:
                    $param_insert['pattern_name'] = '定时发放';
                    $param_insert['timing_start_time'] = $data['start_time'];
                    $param_insert['timing_end_time'] = $data['end_time'];
                    break;
                case 3:
                    $param_insert['pattern_name'] = '下单满多少赠送';
                    if(empty($data['full_price'])){
                        return json(['code' => 0, 'msg' => '满金额不能为空']);
                    }
                    $param_insert['full_price'] = $data['full_price'];
                    break;
                default:
                   break;
            }
            $param_insert['type_status'] = $data['status'];   
            $param_insert['c_time'] = time();   
            $result = db('voucher_type')->insertGetId($param_insert);
            if ($result) {
                return json(['url' => url('Vouchertype/index'), 'code' => 1, 'msg' => '新增成功']);
                // dsLayerOpenSuccess('新增成功');
            } else {
                $this->error(lang('setting_save_fail'));
            }
        }
    }

    /**
     * 详情页
     */
    public function voucher_type_info()
    {
        if (request()->isPost()) {
        } else {
            $id = input('param.id');
            $voucher_info = db('voucher_type')->alias('v')
                 ->join('voucher s','s.voucher_id=v.voucher_id')->where(['id' => $id])->field('v.*,s.voucher_title')->find();
            $this->assign('voucher_info', $voucher_info);
            
            if(!empty($voucher_info['vouchertype_id'])){
                $voucher_type_info = db('voucher_type_info')->where(['id' => $voucher_info['vouchertype_id']])->find();
                $this->assign('voucher_type_info', $voucher_type_info);
            }
            
            return $this->fetch();
        }
    }

        /*
     * 上下架
     */

    public function status()
    {
        $id = trim(input('param.id'));
        $status = trim(input('param.status'));
        if (empty($id)) {
            ds_json_encode(10001, '操作失败');
        }
        $info = db('voucher_type')->where('id', $id)->find();
        if ($status == $info['type_status']) {
            ds_json_encode(10001, '操作失败');
        }
        $data = array();
        $data['type_status'] = $status;
        $rs = db('voucher_type')->where('id', $id)->data($data)->update();
        if ($rs) {
            $this->log(lang('ds_del') . lang('admin_voucher_priceadd') . '[ID:' . $id . ']');
            ds_json_encode(10000, '操作成功');
        } else {
            ds_json_encode(10001, '操作失败');
        }
    }
        /*
     * 删除代金券面额
     */

    public function drop()
    {
        $id = trim(input('param.id'));
        if (empty($id)) {
            ds_json_encode(10001, '操作失败');
        }
        $data = array();
        $data['is_send_del'] = 1;
        $rs = db('voucher_collection')->where('id', $id)->data($data)->update();
        if ($rs) {
            $this->log(lang('ds_del') . lang('admin_voucher_priceadd') . '[ID:' . $id . ']');
            ds_json_encode(10000, lang('ds_common_del_succ'));
        } else {
            ds_json_encode(10001, lang('ds_common_del_fail'));
        }
    }
    

    /**
     * 页面内导航菜单
     *
     * @param string $menu_key 当前导航的menu_key
     * @param array $array 附加菜单
     * @return
     */
    protected function getAdminItemList() {
        $menu_array = array(
            array(
                'name' => 'index',
                'text' => '管理',
                'url' => url('Vouchersend/index')
            ),
            
        );
        return $menu_array;
    }

}
